import { NodeSize } from '../config'

export default function formula(flow) {
  flow.register('Formula', ({ PolygonNode, PolygonNodeModel, h }) => {
    class Node extends PolygonNode {
      getIconShape() {
        return h(
          'svg',
          {
            width: NodeSize,
            height: NodeSize,
            viewBox: '0 0 1024 1024'
          },
          h(
            'path',
            {
              fill: '#3D6480',
              d: 'M525.726476 378.319238H452.266667c9.606095-45.202286 18.212571-77.238857 25.86819-96.426667 14.384762-35.035429 31.183238-49.980952 49.493333-49.980952 3.925333 0 6.509714 0.75581 10.556953 4.949333-3.900952 10.191238-6.192762 18.212571-6.192762 23.430096 0 7.753143 3.023238 14.628571 8.704 20.309333 5.851429 5.583238 13.287619 8.313905 21.845333 8.313905 9.898667 0 18.066286-3.632762 23.917715-11.093334a36.327619 36.327619 0 0 0 8.289523-22.79619c0-12.897524-6.656-23.966476-18.70019-33.158095-11.995429-9.386667-26.940952-13.872762-44.495238-13.872762-38.497524 0.024381-70.509714 20.065524-95.98781 58.831238-16.774095 24.697905-31.622095 61.952-44.81219 111.494095h-62.464l-6.656 31.841524h60.952381l-58.416762 276.772571c-5.680762 26.89219-11.849143 47.689143-18.432 62.634667-12.8 29.013333-27.40419 41.22819-43.032381 41.22819-3.486476 0-5.827048-0.633905-8.094476-3.462095l2.486857-4.754285c2.535619-4.583619 3.900952-9.386667 3.900952-14.262858 0-8.533333-3.096381-15.993905-9.069714-21.967238a32.304762 32.304762 0 0 0-22.479238-8.606476c-9.752381 0-17.724952 3.291429-23.015619 10.532572-4.949333 6.460952-7.558095 13.604571-7.558095 21.357714 0.024381 12.336762 5.973333 23.137524 16.847238 32.377905 10.922667 9.48419 24.868571 14.019048 41.374476 14.019047 36.10819-0.024381 65.438476-18.919619 87.844571-55.369143 22.332952-35.986286 39.960381-82.870857 53.004191-140.531809l47.567238-209.993143h73.557333l6.656-31.817143zM763.538286 421.571048a17.066667 17.066667 0 0 1 3.34019 0.902095c1.999238 0.512 4.388571 1.29219 6.582857 2.169905l8.582096 3.120762c2.608762 1.024 5.290667 1.877333 7.704381 2.511238 2.486857 0.438857 5.339429 0.682667 8.094476 0.682666 7.728762 0 14.384762-2.145524 19.577904-6.631619 5.339429-4.583619 7.68-10.776381 7.68-18.041905-0.024381-10.703238-4.87619-18.846476-15.311238-23.113142-5.485714-2.267429-12.336762-3.267048-20.187428-3.267048-9.094095 0.024381-18.310095 2.730667-27.623619 7.753143-9.337905 5.022476-18.992762 12.53181-29.086476 22.381714-9.923048 9.630476-23.235048 25.283048-39.862858 46.884572l-4.705523-22.113524c-4.85181-22.357333-10.947048-38.326857-18.797715-46.811429-5.802667-5.948952-14.336-8.655238-24.551619-8.655238-6.217143 0-13.58019 0.926476-21.918476 2.681905-8.045714 1.731048-16.847238 4.047238-26.428952 6.92419l-40.935619 12.092953 5.558857 16.579047 22.966857-4.949333c9.094095-1.926095 14.555429-2.730667 16.944762-2.730667 8.216381 0.024381 12.385524 2.023619 14.409143 4.656762 2.755048 3.510857 6.339048 11.678476 10.532571 25.209905 4.169143 13.238857 8.752762 29.379048 13.799619 48.444952 4.315429 16.213333 6.607238 25.404952 7.265524 28.428191-28.111238 35.157333-46.177524 57.344-54.198857 66.511238-12.434286 14.384762-19.821714 22.308571-22.454857 24.454095-1.657905 1.316571-4.461714 2.29181-9.240381 2.29181l-6.802286-1.462857-11.580952-5.412572a41.447619 41.447619 0 0 0-9.557334-3.486476 32.646095 32.646095 0 0 0-9.289143-1.316571 35.59619 35.59619 0 0 0-18.895238 5.583238c-7.070476 4.071619-10.020571 10.849524-10.020571 19.090285 0 6.144 1.560381 11.629714 4.973714 16.384 6.265905 9.362286 17.773714 13.433905 32.841143 13.433905 11.385905 0 21.23581-2.462476 29.281524-7.631238 7.728762-4.87619 18.334476-14.189714 31.524571-27.477333l61.488762-69.607619 17.92 61.732571c3.535238 12.214857 7.289905 21.455238 11.434667 27.306667 7.143619 10.191238 17.286095 15.701333 30.183619 15.701333 19.577905-0.024381 39.058286-11.068952 58.416762-31.085714a243.614476 243.614476 0 0 0 29.866667-38.473143l2.267428-3.535238-17.066667-8.94781-2.633142 3.169524c-12.312381 14.750476-21.357714 24.81981-27.257905 30.281143-5.705143 5.290667-10.581333 7.192381-14.57981 7.192381-2.998857 0-7.582476-2.535619-12.653714-12.775619-2.876952-5.87581-6.558476-16.920381-10.898286-33.426286l-13.555809-49.444571c-3.998476-15.993905-5.802667-24.673524-5.802667-26.916572 0-1.706667 7.241143-12.873143 25.526857-35.718095 18.017524-22.844952 30.305524-31.524571 37.132191-31.524571z'
            }
          )
        )
      }

      getLabelShape() {
        return h(
          'text',
          {
            fill: '#000000',
            fontSize: 12,
            x: NodeSize / 2,
            y: NodeSize + 20,
            textAnchor: 'middle',
            dominantBaseline: 'middle'
          },
          '公式'
        )
      }

      getShape() {
        const { x, y, width, height } = this.getAttributes()
        const transform = `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`
        return h(
          'g',
          {
            transform
          },
          [
            h(
              'rect',
              {
                rx: 5,
                ry: 5,
                width: NodeSize,
                height: NodeSize,
                fill: '#fff',
                stroke: '#3D6380',
                strokeWidth: 1
              }
            ),
            this.getIconShape(),
            this.getLabelShape()
          ]
        )
      }
    }
    class Model extends PolygonNodeModel {
      constructor(data, graphModel) {
        super(data, graphModel)
        this.points = [
          [NodeSize / 2, 0],
          [NodeSize, NodeSize / 2],
          [NodeSize / 2, NodeSize],
          [0, NodeSize / 2]
        ]
      }
    }

    return {
      view: Node,
      model: Model
    }
  })
}
